#include <QCoreApplication>
#include <QPluginLoader>
#include <QDir>
#include <QDebug>
#include <QStringList>
#include "src/global/PluganInterface.h"

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);
    
    qDebug() << "Testing plugin loading...";
    
    // 检查插件目录
    QDir pluginsDir("./plugins");
    if (!pluginsDir.exists()) {
        qDebug() << "Plugins directory does not exist!";
        return -1;
    }
    
    qDebug() << "Plugins directory:" << pluginsDir.absolutePath();
    
    // 列出插件文件
    const auto entryList = pluginsDir.entryList(QDir::Files);
    qDebug() << "Found files:" << entryList;
    
    for (const QString &fileName : entryList) {
        if (!fileName.endsWith(".so")) continue;
        
        qDebug() << "Trying to load:" << fileName;
        
        QPluginLoader loader(pluginsDir.absoluteFilePath(fileName));
        
        // 检查插件是否有效
        if (!loader.isLoaded()) {
            qDebug() << "Plugin not loaded, error:" << loader.errorString();
        }
        
        QObject *plugin = loader.instance();
        if (!plugin) {
            qDebug() << "Failed to get plugin instance:" << loader.errorString();
            continue;
        }
        
        qDebug() << "Plugin loaded successfully:" << plugin->metaObject()->className();
        
        // 尝试转换为接口
        const auto i_pretreatment = qobject_cast<PretreatmentInterface*>(plugin);
        if (!i_pretreatment) {
            qDebug() << "Failed to cast to PretreatmentInterface";
            continue;
        }
        
        qDebug() << "Successfully cast to PretreatmentInterface!";
        
        // 获取预处理列表
        QStringList pretreatments = i_pretreatment->GetPretreatments();
        qDebug() << "Available pretreatments:" << pretreatments;
        
        loader.unload();
    }
    
    return 0;
}
